//
//  AIDiaryReplayBean.swift
//  imprint
//
//  Created by 小柠檬🍋 on 2025/3/11.
//
/**
 * ai日记 - minmax
 */
import Foundation
import SwiftUI
class AICommentMinmaxBean : Codable {
    
    var tokens_to_generate: Int64 = 1034
    var model = "abab6.5s-chat"
    var temperature: Double = 0.9
    var top_p: Double = 0.95
    var messages: [Messages]
    var reply_constraints: ReplyConstraintsc //回复格式
    var bot_setting:[BotSetting] //对每一个机器人的设定
    
    
    init(diary: DiaryBean?, list : [DiaryCommentBean], botRoleBean: RoleBean) {
        let userRoleBean = RoleBean.get(userManager.user, botRoleBean)
        self.reply_constraints = ReplyConstraintsc(aiRole: botRoleBean)
        self.bot_setting = [BotSetting(aiRole: botRoleBean)]
        self.messages = AICommentMinmaxBean.getMessagesList(diary: diary,list: list, botRoleBean: botRoleBean, userRoleBean: userRoleBean)
        
    }
    
    /**
     * 模型回复要求.
     */
    class ReplyConstraintsc : Codable{
        var sender_type = "BOT"
        var sender_name = ""  //指定回复的机器人名称。
        
        init(aiRole: RoleBean) {
            self.sender_name = aiRole.name ?? ""
        }
        
    }
    
    /**
     * 对每一个机器人的设定。
     * @param content 具体机器人的设定,长度影响接口性能。
     */
    class BotSetting : Codable {
        var bot_name: String
        var content: String
        init(aiRole: RoleBean) {
            bot_name = aiRole.name ?? ""
            content = RoleBean.getBotDesc(aiRole)
        }
    }
    
    class Messages: Codable {
        var sender_type = "USER"
        var text: String
        var sender_name: String
        
        init(_ content: String,_ botRoleBean: RoleBean) {
            var name = AiChatExt.getName2(botRoleBean)
            if name.isEmpty == true {
                name = "亲爱的"
            }
            self.text = content
            self.sender_name = name
        }
    }
    
    private static func getMessagesList(diary: DiaryBean?, list : [DiaryCommentBean], botRoleBean: RoleBean,
                                        userRoleBean: RoleBean) ->[Messages] {
        let time = Date().timeIntervalSince1970
        //动作描述
        let actionDescClose = !AppStorageManager.shared.toggleAiDesc ? "回复只输出最后的话语，不包括任何表情动作" : ""
        let headDesc = configManager.getConfig().getAiDescribe().headDescribe ?? ""
        let botDesc =  RoleBean.getBotDesc(botRoleBean)
        let userDesc =  RoleBean.getUserDesc(botRoleBean)
        let ruleDesc = "\(headDesc)\(configManager.getConfig().getAiDescribe().chatDescribe ?? "")\(actionDescClose)"
        let enDesc = ""
        let desc = "\(botDesc)\n\n---\n\(userDesc)\n\n\(enDesc)"
        
        var listMsg : [Messages] = []
        listMsg.append(Messages(desc.formatAiChat(botRoleBean,time), botRoleBean))
        
        listMsg.append(Messages(ruleDesc.formatAiChat(botRoleBean,time),userRoleBean))
        
        //先添加日记内容
        if (diary != nil) {
            let roleBean = diary?.roleType == IRoleType.BOT.rawValue ? botRoleBean : userRoleBean
            listMsg.append(Messages(ruleDesc.formatAiChat(botRoleBean,time),userRoleBean))
        }
        list.forEach { it in
            if let role = (it.role) {
                listMsg.append(Messages(it.content ?? "", role))
            }
        }
        return listMsg
    }
    
    
}
